Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo

Imports Phoenix.Events

Namespace Forms.Area

    Public Class GridAreaBase
        Inherits AreaBase


#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
        End Sub

        'UserControl overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            components = New System.ComponentModel.Container
        End Sub

#End Region

#Region " Area Behaviour "
        Public WithEvents GridView As GridView

        Public Overridable ReadOnly Property Icon() As DevExpress.XtraEditors.Repository.RepositoryItemImageComboBox
            Get
                Return Nothing
            End Get
        End Property

        Public Overridable ReadOnly Property IconColumn() As DevExpress.XtraGrid.Columns.GridColumn
            Get
                Return Nothing
            End Get
        End Property


        Private Shared EmptyList As ArrayList = New ArrayList

        Private mSingleItem As New ArrayList
        Private Function SingleItem(ByVal item As DataRow) As IList
            mSingleItem.Clear()
            mSingleItem.Add(item)
            Return mSingleItem
        End Function

        Public Overrides ReadOnly Property SelectedItems() As IList
            Get
                If GridView.SelectedRowsCount > 1 Then
                    Dim list As New ArrayList
                    Dim iList As Integer() = GridView.GetSelectedRows
                    Dim size As Integer = iList.Length
                    Dim i As Integer
                    For i = 0 To size - 1
                        list.Add(GridView.GetDataRow(iList.GetValue(i)))
                    Next
                    Return list 'GridView.GetSelectedRows
                ElseIf Helpers.IsNotInvalidRowHandle(GridView.FocusedRowHandle) Then
                    If GridView.FocusedRowHandle > -1 Then
                        Return SingleItem(GridView.GetDataRow(GridView.FocusedRowHandle))
                    Else
                        Return EmptyList
                    End If
                Else
                    Return EmptyList
                End If
            End Get
        End Property

        Public Overrides Sub SelectAll()
            GridView.SelectAll()
            GridView.Invalidate()
        End Sub

        Public Overrides Function Copy(ByVal arr As IList) As IList
            Dim list As New ArrayList
            For Each item As DataRow In arr
                If Not item Is Nothing Then
                    list.Add(item)
                End If
            Next
            Return list
        End Function

        Public Overrides Sub Cut(ByVal arr As IList)
            Throw New InvalidOperationException
        End Sub

        Public Overrides Sub DoDefaultAction()
            If DefaultAction Is Nothing Then Return

            If GridView.SelectedRowsCount > 1 Then

                For Each h As Integer In GridView.GetSelectedRows
                    DefaultAction(CType(GridView.GetRow(h), DataRowView).Row)
                Next
            ElseIf Helpers.IsNotInvalidRowHandle(GridView.FocusedRowHandle) Then
                DefaultAction(CType(GridView.GetRow(GridView.FocusedRowHandle), DataRowView).Row)
            End If
        End Sub


#End Region

#Region " Event Handlers "
        Protected Overrides Sub OnLoad(ByVal e As EventArgs)
            MyBase.OnLoad(e)

            If DesignMode Then Return

            GridView.GroupFormat = "{1}"

            UpdateConfiguration()
        End Sub

        Protected Overrides Sub OnApplicationEvent(ByVal sender As Object, ByVal e As Events.PhoenixEventArgs)
            Select Case e.EventCode
                Case PhoenixEventCode.ConfigurationChanged
                    UpdateConfiguration()
            End Select
        End Sub

        Public Overridable Sub UpdateConfiguration()
            GridView.OptionsView.ShowFilterPanel = True
            If Not Icon Is Nothing Then
                Icon.SmallImages = AppSharedComponents.ListImages
            End If

            If Not IconColumn Is Nothing Then
                IconColumn.MinWidth = AppSharedComponents.ListImages.ImageSize.Width + AppIconColumnPadding
            End If
        End Sub

        Private Sub GridView_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GridView.MouseUp
            If e.Button = MouseButtons.Right Then
                Dim hi As GridHitInfo = GridView.CalcHitInfo(New Point(e.X, e.Y))
                Select Case hi.HitTest
                    Case GridHitTest.EmptyRow
                    Case GridHitTest.Row
                    Case GridHitTest.RowCell
                    Case GridHitTest.RowEdge
                    Case GridHitTest.RowFooter
                    Case Else
                        Return
                End Select

                If GridView.SelectedRowsCount > 1 Then
                    If Helpers.IsNotInvalidRowHandle(hi.RowHandle) Then
                        Dim ins As Integer() = GridView.GetSelectedRows
                        If Array.IndexOf(ins, hi.RowHandle) = -1 Then
                            For Each rh As Integer In ins
                                GridView.UnselectRow(rh)
                            Next
                            GridView.FocusedRowHandle = hi.RowHandle
                        End If
                    End If
                Else
                    GridView.FocusedRowHandle = hi.RowHandle
                End If

                ShowContextMenu()
            End If
        End Sub

        Private Sub GridView_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles GridView.KeyPress
            If e.KeyChar = Microsoft.VisualBasic.vbCr Then
                DoDefaultAction()
            End If
        End Sub

        Private Sub GridView_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles GridView.MouseDown
            CType(AppMainWindow.ActiveView, View.MainView).ActiveArea = Me
            If e.Clicks >= 2 And e.Button = MouseButtons.Left Then
                Dim hi As GridHitInfo = GridView.CalcHitInfo(New Point(e.X, e.Y))
                Select Case hi.HitTest
                    Case GridHitTest.EmptyRow
                    Case GridHitTest.Row
                    Case GridHitTest.RowCell
                    Case GridHitTest.RowEdge
                    Case GridHitTest.RowFooter
                    Case Else
                        Return
                End Select
                DoDefaultAction()
            End If
        End Sub
#End Region

    End Class

End Namespace